import { queryIntegratedList, actionEquipmentWiteList,actionEquipmentScrap,actionEquipmentDel, actionEquipmentExport, actionEquipmentImport,saveAddEquipment,saveEditEquipment,
  queryEquipmentInfo, queryRepairLog, queryMaintenlog, queryOperationlog, actionEquipmentWiteListCancel
} from '../services/equipment';

export default {
  namespace: 'equipment_integrated',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    detaildata:{},
    log:{
      repair:{},
      mainten:{},
      operation:{},
    },
    loading: {
      integrated_list: true,
      model_submit:false,
      detailinfo:true,
    },
  },

  effects: {
    // 获取列表信息
    *fetch_integrated_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { integrated_list: true },
      });
      const response = yield call(queryIntegratedList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { integrated_list: false },
      });
    },

    // 批量操作加入白名单
    *action_equipment_witelist({ payload }, { call, put }) {
      const response = yield call(actionEquipmentWiteList, payload);
    },

    // 取消白名单
    *action_equipment_witelist_cancel({ payload }, { call, put }) {
      const response = yield call(actionEquipmentWiteListCancel, payload);
    },

    // 批量操作报废
    *action_equipment_scrap({ payload }, { call, put }) {
      const response = yield call(actionEquipmentScrap, payload);
    },
    // 批量操作删除
    *action_equipment_del({ payload }, { call, put }) {
      const response = yield call(actionEquipmentDel, payload);
    },



    // 文件导出
    *action_equipment_export({ payload }, { call, put }) {
      const response = yield call(actionEquipmentExport, payload);
    },
    // 文件导入
    *action_equipment_import({ payload }, { call, put }) {
      const response = yield call(actionEquipmentImport, payload);
    },





    // 新增产品设备
    *add_equipment({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(saveAddEquipment,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 修改产品设备
    *edit_equipment({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(saveEditEquipment,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },


    // 获取单个产品详情
    *fetch_equipment_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });

      const response = yield call(queryEquipmentInfo, payload);
      response &&  (
        yield put({
          type: 'appendDataDetail',
          payload:response.data,
        })
      )
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },


    // 获取单个设备维修日志
    *fetch_repair_log({ payload }, { call, put }) {
      const response = yield call(queryRepairLog, payload);
      response && (
        yield put({
          type: 'appendDataLog',
          payload:{repair:response.data},
        })
      );

    },

    // 获取单个设备养护日志
    *fetch_mainten_log({ payload }, { call, put }) {
      const response = yield call(queryMaintenlog, payload);
      response && (
        yield put({
          type: 'appendDataLog',
          payload:{mainten:response.data},
        })
      );

    },

    // 获取单个设备操作日志
    *fetch_operation_log({ payload }, { call, put }) {
      const response = yield call(queryOperationlog, payload);
      response && (
        yield put({
          type: 'appendDataLog',
          payload:{operation:response.data},
        })
      );

    },
  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },

    appendDataLog(state, action) {
      return {
        ...state,
        log: {
          ...state.log,
          ...action.payload,
        },
      };
    },

    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        detaildata:{},
        log:{},
        loading:{}
      };
    },

  },
};
